package com.mlz.controller;

import com.mlz.pojo.Result;
import com.mlz.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

//文件上传
@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        //调用阿里云oss工具类,来完成上传文件存入到阿里云
        String url = aliOSSUtils.upload(image);
        //将图片上传完成后所得到的url返回给前端页面
        return Result.success(url);
    }



   /* @PostMapping("/upload")
    public Result upload(String username, Integer age,  MultipartFile image) throws IOException {
        //日志文件
        log.info("文件上传: {},{},{}",username,age,image);

        //获取源文件的名称
        String originalFilename = image.getOriginalFilename();
        //1.png
        System.out.println(originalFilename);

        //构建新的文件名
        //扩展名 png
        String extname = originalFilename.substring(originalFilename.lastIndexOf("."));

        //随机名称 + 扩展名
        String newFileName = UUID.randomUUID().toString() + extname;
        //将文件存储到服务器的磁盘文件
        image.transferTo(new File("D:/images/" + newFileName));

        return Result.success();
    }*/























}
